package com.hnevc.xiewenguang;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 *
 * 使用JDBC访问数据库
 * 数据库project
 * 数据库表 user
 */
public class Example01 {

    public static void main(String[] args) {
        try {
            //通过Driver接口注册数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3307/project";
            String usernem = "root";
            String password = "root";
            //创建数据库连接
            Connection connection = DriverManager.getConnection(url,usernem,password);
            //获取连接后对象的statem对象
            Statement statement = connection.createStatement();
            String sql = "select * from user"; //查询user表中的所有数据
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()){
                String id = resultSet.getString("id");
                String name = resultSet.getString("name");
                System.out.println(id+"|" +name);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}